package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.Result;
import com.example.reggie.domain.Category;
import com.example.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/category")
@RestController
public class CategoryController {
    @Autowired
    CategoryService categoryService;


    // 菜品及套餐分类的分页查询
    @GetMapping("/page")
    Result<Page> categoryPageList(Integer page,Integer pageSize){
        Page<Category> categoryPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(categoryPage,queryWrapper);
        return Result.success(categoryPage);
    }


    // 菜品或套餐分类添加接口
    @PostMapping
    Result<String> categoryPackageAdd(@RequestBody Category category){
        categoryService.save(category);
        return Result.success("添加成功");
    }

    // 菜品或套餐分类修改接口
    @PutMapping
    Result<String> categoryUpdate(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.success("修改成功");
    }

    // 通过id删除菜品或套餐
    @DeleteMapping
    Result<String> categoryDeleteById(long ids){
        categoryService.removeById(ids);
        return Result.success("删除成功");
    }

    // 查询全部菜品分类或全部套餐分类
    @GetMapping("/list")
    Result<List> categoryTypeList(Integer type,Long categoryId){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type!=null,Category::getType,type);
        queryWrapper.eq(categoryId!=null,Category::getId,categoryId);
        List<Category> list = categoryService.list(queryWrapper);
        return Result.success(list);
    }
}
